<!--六位验证码输入框组件-->
<template>
  <div class="security-code-wrap" :style="{height: bodyHeight}">
    <div class="top">
      <div class="title">欢迎体验密马内测版</div>
      <p>为虚拟商品（不发快递）交易量身定做的APP，卖货零门槛、自动发货、超低服务费，诚邀您体验！</p>
    </div>
    <div class="desc">请输入内测码</div>
    <label for="code">
      <ul class="security-code-container">
        <li class="field-wrap" v-for="(item, index) in number" :key="index">
          <span v-if="fouc">
            <i class="char-field" v-if="value.length === 0">{{ index === 0 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 1">{{ index === 1 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 2">{{ index === 2 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 3">{{ index === 3 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 4">{{ index === 4 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 5">{{ index === 5 ? beet : value[index] }}</i>
            <i class="char-field" v-if="value.length === 6">{{ value[index] }}</i>
          </span>
          <span v-else>
            <i class="char-field">{{ value[index] }}</i>
          </span>
        </li>
      </ul>
    </label>
    <div class="btn" @click="rietNow">立即下载</div>
    <div class="foot">内测QQ交流群：<span>1071086035</span></div>
    <input
      ref="input"
      class="input-code"
      @keyup="handleInput($event)"
      @focus="foucus"
      @blur="blur"
      v-model="value"
      id="code"
      name="code"
      type="tel"
      :maxlength="number"
      autocorrect="off"
      autocomplete="off"
      autocapitalize="off"
    />
    <van-popup v-model="show" round>
      <div class="Ios">
        <div class="title">温馨提示</div>
        <p>您的设备为iOS系统，请联系客服下载密马内测版。</p>
        <ul>
          <li>
            微信客服
            <span>18811879591</span>
          </li>
          <li>
            QQ客服
            <span>279891830</span>
          </li>
        </ul>
        <div class="ikonw" @click="iknow">知道了</div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { inviteTest } from '@/api/index'
import { Toast } from 'vant';
export default {
  name: "SecurityCode",
  // component properties
  props: {
    number: {
      type: Number,
      default: 6
    },
    placeholder: {
      type: String,
      default: ''
    }
  },
  // variables
  data() {
    return {
      value: "",
      beet: '|',
      fouc: false,
      timer: {},
      isIos: false,
      isAndroid: true,
      bodyHeight: '100vh',
      codeYes: true,
      show: false
    };
  },
  created () {
    this.bodyHeight = document.documentElement.clientHeight + 'px'
    console.log(this.bodyHeight)
    let u = navigator.userAgent;
    this.isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    this.isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (this.isiOS) {
      this.show = true
    }
    let bool = true
    this.timer = setInterval(() => {
      bool = !bool
      bool ? this.beet = '' : this.beet = '|'
    }, 600);
  },
  methods: {
    iknow () {
      this.show = false
    },
    rietNow () {
      this.hideKeyboard()
    },
    blur () {
      this.fouc = false
      clearInterval(this.timer)
    },
    foucus () {
      let bool = true
      this.timer = setInterval(() => {
        bool = !bool
        bool ? this.beet = '' : this.beet = '|'
      }, 600);
      this.fouc = true
    },
    hideKeyboard() {
      // 输入完成隐藏键盘
      if (this.isAndroid) {
        if (this.value.length < 6) {
          if (this.value.length == 0) {
            Toast('请输入内测码')
          } else {
            Toast('请输入正确的内测码')
          }
        } else {
          inviteTest({ code: this.value }).then(res => {
            if (res.code === 0) {
              Toast('验证成功')
              // setTimeout(() => {
                // window.open(res.data.url)
                window.location.href = res.data.url
              // }, 1500);
            }
          }, err => {
            Toast(err.msg)
            this.codeYes = false
          })

        }

      }
      if (this.isiOS) {
        this.show = true
      }
      document.activeElement.blur(); // ios隐藏键盘
      this.$refs.input.blur(); // android隐藏键盘
    },
    handleSubmit() {
      // this.$emit("input", this.value);
      // alert(this.value)
    },
    handleInput(e) {
      console.log(this.$refs.input.value)
        this.$refs.input.value = this.value;
        if (!this.codeYes) {
          this.$refs.input.value = ''
          this.value = ''
          this.codeYes = true
        }
        if (this.value.length >= this.number) {
          this.hideKeyboard();
        }
        this.handleSubmit();
    }
  }
};
</script>

<style scoped lang="scss">
.security-code-wrap {
  overflow: hidden;
  text-align: center;
  background: #ffffff;
  // height: 100vh;
  position: relative;
}
.Ios {
  width: 258px;
  height: 233px;
  padding: 26px 20px 17px;
  border-radius: 10px;
  overflow: hidden;
  text-align: left;
  .title {
    font-size: 17px;
    color: #000000;
    text-align: center;
    margin-bottom: 8px;
  }
  p {
    font-size: 13px;
    color: #666666;
    line-height: 24px;
  }
  ul {
    li {
      color: #666666;
      font-size: 13px;
      line-height: 24px;
      span {
        color: #FF7C00;
      }
    }
  }
  .ikonw {
    width: 100%;
    height: 46px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #ffdd07;
    font-size: 14px;
    color: #2f2f2f;
    border-radius: 30px;
    margin-top: 20px;
  }
}
.btn {
  width: 345px;
  height: 52px;
  color: #2F2F2F;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #FFDD07;
  border-radius: 40px;
  margin: 70px auto 0;
}
.foot {
  font-size: 13px;
  color: #666666;
  font-weight: 400;;
  position: absolute;
  bottom: 36px;
  width: 100%;
  span {
    color: #FF7C00;
  }
}
.top {
  padding: 57px 24px 0 24px;
    text-align: left;
  .title {
    font-size: 24px;
    line-height: 24px;
    font-weight: 600;
    color: #1e1e1e;
  }
  p {
    color: #666666;
    font-size: 13px;
    line-height: 18px;
    margin-top: 23px;
  }
}
.desc {
  margin-top: 60px;
  font-size: 18px;
  font-weight: 600;
  color: #1e1e1e;
  text-align: left;
  margin-left: 24px;
  margin-bottom: 10px;
}
.security-code-container {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 22px;
  .field-wrap {
    list-style: none;
    display: block;
    width: 48px;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    background-color: #fff;
    margin: 2px;
    color: #000;
    border:0.5px solid rgba(226,226,226,1);
    border-radius: 4px;
    .char-field {
      font-style: normal;
    }
  }
}
.input-code {
  position: absolute;
  left: -9999px;
  top: -99999px;
  width: 200px;
  height: 0;
  opacity: 0;
  z-index: -1;
}
</style>
